using System;
using System.Windows.Forms;

namespace EnhancedCalculator
{
    public class CalculatorForm : Form
    {
        private TextBox displayTextBox;
        private string currentInput = "";
        private string previousInput = "";
        private string currentOperator = "";
        private bool newCalculation = true;
        public CalculatorForm()
        {
            InitializeComponents();
            this.Text = "简易计算器";
            this.Size = new System.Drawing.Size(320, 450);
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
        }

        private void InitializeComponents()
        { // 显示文本框 
            displayTextBox = new TextBox
            {
                Location = new System.Drawing.Point(10, 10),
                Size = new System.Drawing.Size(280, 60),
                Font = new System.Drawing.Font("Microsoft Sans Serif", 18),
                TextAlign = HorizontalAlignment.Right,
                ReadOnly = true,
                Multiline = true
            };
            this.Controls.Add(displayTextBox);
            // 数字按钮
            for (int i = 0; i < 10; i++)
            {
                Button numberButton = new Button
                {
                    Text = i.ToString(),
                    Tag = i,
                    Size = new System.Drawing.Size(65, 65),
                    Font = new System.Drawing.Font("Microsoft Sans Serif", 14)
                };
                numberButton.Click += NumberButton_Click;
                // 数字按钮
                if (i == 0)
                {
                    numberButton.Location = new System.Drawing.Point(10, 340);
                }
                else
                {
                    int row = (9 - i) / 3;
                    int col = (i - 1) % 3;
                    numberButton.Location = new System.Drawing.Point(10 + col * 75, 260 - row * 80);
                }
                this.Controls.Add(numberButton);
            }
            // 运算符按钮 
            string[] operators = { "+", "-", "×", "÷" };
            for (int i = 0; i < operators.Length; i++)
            {
                Button operatorButton = new Button
                {
                    Text = operators[i],
                    Tag = operators[i],
                    Size = new System.Drawing.Size(65, 65),
                    Font = new System.Drawing.Font("Microsoft Sans Serif", 14),
                    Location = new System.Drawing.Point(235, 80 + i * 80)
                };
                operatorButton.Click += OperatorButton_Click;
                this.Controls.Add(operatorButton);
            }
            // 等号按钮
            Button equalsButton = new Button
            {
                Text = "=",
                Size = new System.Drawing.Size(65, 65),
                Font = new System.Drawing.Font("Microsoft Sans Serif", 14),
                Location = new System.Drawing.Point(160, 340)
            };
            equalsButton.Click += EqualsButton_Click;
            this.Controls.Add(equalsButton);

            // 清空按钮
            Button clearButton = new Button
            {
                Text = "C",
                Size = new System.Drawing.Size(65, 65),
                Font = new System.Drawing.Font("Microsoft Sans Serif", 14),
                Location = new System.Drawing.Point(85, 340),
            };
            clearButton.Click += ClearButton_Click;
            this.Controls.Add(clearButton);
        }

        private void NumberButton_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            
            if (newCalculation)
            {
                currentInput = button.Tag.ToString();
                newCalculation = false;
            }
            else
            {
                currentInput += button.Tag.ToString();
            }
            
            UpdateDisplay();
        }

        private void OperatorButton_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            string op = button.Tag.ToString();

            if (!string.IsNullOrEmpty(currentInput))
            {
                if (!string.IsNullOrEmpty(previousInput) && !newCalculation)
                {
                    Calculate();
                }
                previousInput = currentInput;
                currentOperator = op;
                currentInput = "";
                UpdateDisplay();
            }
        }

        private void EqualsButton_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(previousInput) && !string.IsNullOrEmpty(currentInput) && !string.IsNullOrEmpty(currentOperator))
            {
                Calculate();
                newCalculation = true;
            }
        }

        private void ClearButton_Click(object sender, EventArgs e)
        {
            currentInput = "";
            previousInput = "";
            currentOperator = "";
            newCalculation = true;
            displayTextBox.Text = "";
        }

        private void Calculate()
        {
            double num1, num2;
            if (!double.TryParse(previousInput, out num1) || !double.TryParse(currentInput, out num2))
                return;

            double result = 0;
            bool error = false;
            string operationSymbol = currentOperator;

            switch (currentOperator)
            {
                case "+":
                    result = num1 + num2;
                    break;
                case "-":
                    result = num1 - num2;
                    break;
                case "×":
                    result = num1 * num2;
                    break;
                case "÷":
                    if (num2 != 0)
                    {
                        result = num1 / num2;
                    }
                    else
                    {
                        error = true;
                    }
                    break;
            }

            if (error)
            {
                displayTextBox.Text = $"{previousInput}{operationSymbol}{currentInput}=错误";
            }
            else
            {
                displayTextBox.Text = $"{previousInput}{operationSymbol}{currentInput}={result}";
                previousInput = result.ToString();
            }
            
            currentInput = "";
            currentOperator = "";
        }

        private void UpdateDisplay()
        {
            if (!string.IsNullOrEmpty(currentOperator))
            {
                displayTextBox.Text = $"{previousInput}{currentOperator}{currentInput}";
            }
            else
            {
                displayTextBox.Text = currentInput;
            }
        }

        [STAThread]
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new CalculatorForm());
        }
    }
}